var ns, app, dialog, toast;
app = new Vue({
    el: '#app',
    data: {
        list: [],
        form: {
            ID:0,
            pageindex: 0,
            pagesize: 10,
            isFirstLoad: 1,
            keywords: '',
            status: 0, //状态（0-通知我的 1-我发起的)
            IsRead:false,//仅看已读1  未读0
        },
        current_item_length: 0,
        is_searching: false,
        scroll_top: 0,
        can_scroll: false,
        showsearchparam: false,
        showsearch:false,
        showTop:false,
        searchlist:[],
        number:0,
        checked:false,
        Read:false,
    },
    methods: {
        get_data: function () {
            var that = this;
            that.get_NoRead();//获取未读数量

            that.is_searching = true;
            that.can_scroll = false;
            var options = {};
            options.action = "getinternallist";
            options.pagesize = that.form.pagesize;
            options.pageindex = that.form.pageindex;
            options.Keywords = that.form.keywords;
            options.Initiated = that.form.status;
            options.IsRead = 2;
            console.log(JSON.stringify(options));
            ns.post(options, function (succeed, data, err) {
                console.log(JSON.stringify(data));
                that.is_searching = false;
                if (succeed) {

                    // if (data.data.length == that.form.pagesize) {
                    //     that.can_scroll = true;
                    // }
                    // if (that.form.pageindex == 0) {
                    //     that.list =[];
                    //     setTimeout(() => {
                    //         window.scrollTo(0,document.body.scrollHeight);
                    //     }, 300);
                    // }
                    // data.data.forEach(function(item){
                    //     that.list.splice(0,0,item);
                    // })
                    // if(that.form.pageindex >= 10){
                    //     setTimeout(() => {
                    //         that.do_scrollbtn(data.data[0].ID);
                    //         toast.hide();
                    //     }, 100);
                    // }
                    if (data.data.length == that.form.pagesize) {
                        that.can_scroll = true;
                    }
                    if (that.form.pageindex == 0) {
                        that.list = data.data;
                    } else {
                        that.list = that.list.concat(data.data);
                    }

                    that.current_item_length = that.list.length;

                    that.form.isFirstLoad = 0;

                    // for(var i=0;i<data.data.length;i++){
                    //     data.data[i].AddTime=ns.SetTime_style(data.data[i].AddTime);
                    // }
                    if(that.form.ID){
                        setTimeout(function() {
                            that.do_scrollTo();
                        }, 1000)
                    }
                    api.sendEvent({
                        name:'isreadCustomer'
                    })
                    
                } else if (err) {
                    that.current_item_length = 0;
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },
        choose_noread:function(){
            var that=this;
            that.Read=!that.Read;
            that.get_data();
            // that.checked=!that.checked;
            // console.log(JSON.stringify(that.checked));
        },
        open_renwu:function(){
            ns.openWin('neibutz_renwulist', '内部报事待办');
        },
        //翻页第一条数据的位置
        do_scrollbtn:function(id){
    　　　　 var sollid = document.getElementById('scroll'+id);
    　　　　 window.scrollTo(0,sollid.offsetTop); 
    　　},
        do_open_detail: function (item) {
            var that = this;
            var options={};
            options.action="jpushlogusersetread";
            options.JpushLogID=item.JpushLogID;
            ns.post(options, function (succeed, data, err) {
                if(succeed){
                    that.get_data();
                }
                
            });
            ns.openWin('neibutz_detail_frm', '内部通知详情', {
                id: item.ID,
                ids: item.IDs || 0,
                IsRead: item.IsRead,
                TypeStatus: 0
            });
        },
        doHideSearch: function () {
            var that = this;
            that.showsearchparam = false;
            that.showsearch = false;
        },
        // 确认收到
        do_getok: function (item) {
            var that = this;
            var options = {};
            options.IDs = item.IDs;
            options.tdRemark='收到';
            options.action = "savanotificationread";
            ns.post(options, function (succeed, data, err) {
                if (succeed) {  
                    that.do_open_detail(item);
                    api.sendEvent({
                        name:'reloadneibulist'
                    })

                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        open_select:function(){
            var that=this;
            that.showTop=!that.showTop;
        },
        doHideSearch:function(){
            var that=this;
            that.showTop=false;
            that.showsearch=false;
        },
        get_NoRead:function(){
            var that=this;
            var options = {};
            options.action = "getinternallist";
            options.pagesize = 1000;
            options.pageindex =0;
            options.Keywords = that.form.keywords;
            options.Initiated = 0;
            options.IsRead = 0;
            ns.post(options, function (succeed, data, err) {
                console.log(JSON.stringify(data));
                that.is_searching = false;
                if (succeed) {
                    that.searchlist = data.data;  
                    that.number=  that.searchlist.length;
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },
        select_allstatus:function(){
            var that=this;
            that.showsearch=!that.showsearch;
            that.get_NoRead();
        },
        //搜索结果返回位置
        do_scrollTo:function(){
            var that = this;
    　　　　 var sollid = document.getElementById('scroll'+that.form.ID);
    　　　　 window.scrollTo(0,sollid.offsetTop); 
            var classlist=document.getElementById('scroll'+that.form.ID).getElementsByClassName('boxbg')[0];
            classlist.style.backgroundColor = "#eaf1ff";
            setTimeout(function() {
                classlist.style.backgroundColor = "#f7fbff";
            }, 800)
            
    　　},
    }
});
apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    dialog = new auiDialog();
    toast = new auiToast();

    app.form.ID = api.pageParam.ID || 0;

    api.addEventListener({
        name: 'reloadneibulist'
    }, function (ret, err) {
        app.form.isFirstLoad = 1;
        app.get_data();
    });
    // ns.init_pull(true,true);
    ns.init_pull();
    app.get_data();
};
